package exam.one;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;

import db.DBUtil;
import io.MyIoper;

import javax.swing.JButton;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.Vector;
import java.awt.event.ActionEvent;
import javax.swing.JTable;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.DefaultComboBoxModel;

import javab.Actable;
import javab.EnTypeSearch;
import javab.Infotable;
import thread.DrawThread;
import thread.SaveThread;

public class MyExamGUI extends JFrame {

	private JPanel contentPane;
	private JTextField textField;
	private JTextField textField_1;
	private JTable table;
	DBUtil dbu=new DBUtil();
	MyIoper myi=new MyIoper();
	ArrayList<Actable> arlistactable=new ArrayList<Actable>();
	ArrayList<Infotable> arlistinfotable=new ArrayList<Infotable>();
	
	Connection con;
	DrawThread dt;
	SaveThread st;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MyExamGUI frame = new MyExamGUI();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public MyExamGUI() {
		
		
		setTitle("Java2018\u7EA7\u8003\u8BD5\u7A97\u53E3");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 719, 409);
		
		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		
		JMenu mnFile = new JMenu("\u6587\u4EF6");
		menuBar.add(mnFile);
		
		JMenuItem mntmSave = new JMenuItem("\u5E8F\u5217\u5316\u4FDD\u5B58");
		
		mnFile.add(mntmSave);
		
		JMenuItem menuItem = new JMenuItem("\u53CD\u5E8F\u5217\u5316\uFF08\u53EF\u9009\uFF09");
		mnFile.add(menuItem);
		
		JMenu mnDb = new JMenu("\u6570\u636E\u5E93");
		menuBar.add(mnDb);
		
		JMenuItem mntmConnect = new JMenuItem("\u8FDE\u63A5");
		
		//数据库连接
		mntmConnect.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				con=dbu.getDBConnection("mysql");
				if(con!=null) {
					JOptionPane.showMessageDialog(null, "数据库连接成功");
				}else {
					JOptionPane.showMessageDialog(null, "数据库连接错误");
				}
			}
		});
		mnDb.add(mntmConnect);
		
		JMenuItem mntmDi = new JMenuItem("\u65AD\u5F00");
		
		//数据库断开
		mntmDi.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(con==null) {
					JOptionPane.showMessageDialog(null, "未连接数据库");
				}else {
					dbu.closeAll();
					con=null;                 //当数据库断开时，con不为空，为了后序操作的判断，将con=null，用于判断数据库当前的连接状态
					JOptionPane.showMessageDialog(null, "数据库已断开");
				}
			}
		});
		
		mnDb.add(mntmDi);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder(null, "\u53D6\u94B1", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel.setBounds(28, 49, 256, 78);
		contentPane.add(panel);
		panel.setLayout(null);
		
		JButton button = new JButton("\u53D6\u94B1");
		
		
		
		
		button.setBounds(148, 28, 93, 23);
		panel.add(button);
		
		textField = new JTextField();
		textField.setBounds(10, 29, 128, 21);
		panel.add(textField);
		textField.setColumns(10);
		
		JPanel panel_1 = new JPanel();
		panel_1.setBorder(new TitledBorder(null, "\u5B58\u94B1", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel_1.setBounds(28, 164, 256, 83);
		contentPane.add(panel_1);
		panel_1.setLayout(null);
		
		textField_1 = new JTextField();
		textField_1.setBounds(10, 30, 123, 21);
		panel_1.add(textField_1);
		textField_1.setColumns(10);
		
		JButton btnNewButton = new JButton("\u5B58\u94B1");
		
		btnNewButton.setBounds(153, 29, 93, 23);
		panel_1.add(btnNewButton);
		
		JPanel panel_2 = new JPanel();
		panel_2.setBorder(new TitledBorder(null, "\u4EA4\u6613\u5386\u53F2", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel_2.setBounds(294, 10, 399, 278);
		contentPane.add(panel_2);
		panel_2.setLayout(null);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 73, 379, 195);
		panel_2.add(scrollPane);
		
		table = new JTable();
		scrollPane.setViewportView(table);
		
		JComboBox comboBox = new JComboBox();
		comboBox.setModel(new DefaultComboBoxModel(EnTypeSearch.values()));
		comboBox.setBounds(115, 25, 138, 21);
		panel_2.add(comboBox);
		
		JLabel label = new JLabel("\u9009\u62E9\u67E5\u8BE2\u6761\u4EF6\uFF1A");
		label.setBounds(10, 28, 95, 15);
		panel_2.add(label);
		
		JButton button_1 = new JButton("\u67E5\u8BE2");
		
		button_1.setBounds(277, 24, 93, 23);
		panel_2.add(button_1);
		
		JLabel label_1 = new JLabel("\u53EF\u4EE5\u4F5C\u4E3A\u64CD\u4F5C\u63D0\u9192\u7684\u72B6\u6001\u680F\uFF0C\u53EF\u4EE5\u6709\u9009\u62E9\u4F7F\u7528");
		label_1.setBounds(29, 313, 360, 26);
		contentPane.add(label_1);
		
		//取钱
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String input=textField.getText().trim();
				if(con==null) {										//判断数据库是否已断开
					JOptionPane.showMessageDialog(null, "请先连接数据库");
				}else {
					if(input.length()==0) {							//判断输入是否为空
						JOptionPane.showMessageDialog(null, "请先输入金额");
					}else {
						int num=Integer.valueOf(input);
						arlistactable=dbu.queryById();
						if(arlistactable.get(0).getBalance()<num) {
							JOptionPane.showMessageDialog(null, "余额不足");
						}else {
							if(num%100!=0) {
								JOptionPane.showMessageDialog(null, "取款只能是100的整数倍");
							}else {
								JOptionPane.showMessageDialog(null, "正在取款，请稍后");
								Actable a=arlistactable.get(0);
								dt=new DrawThread(a, num);
								
								dt.start();
//								if(dt.isAlive()) {
//									label_1.setText("正在取款，请稍后...");
//								}
								
								//使数据更新在操作完后进行在线程结束后
								try {
									dt.sleep(4100);
								} catch (InterruptedException e1) {
									// TODO Auto-generated catch block
//									e1.printStackTrace();
									JOptionPane.showMessageDialog(null, "线程错误");
									
								}
								
								arlistinfotable.clear();
								arlistinfotable=dbu.query();
								Vector<String> v=new Vector<String>();
								v.add("Timest");
								v.add("Opertype");
								v.add("Money");
								DefaultTableModel df=new DefaultTableModel(v,0);
								for(Infotable in:arlistinfotable) {
									String[] line= {in.getTimest(),in.getOpertype(),String.valueOf(in.getMoney())};
									df.addRow(line);
								}
								table.setModel(df);
								
							}
							
						}
					}
				}
			}
		});
		
				//存钱
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String input=textField_1.getText().trim();
				if(con==null) {
					JOptionPane.showMessageDialog(null, "请先连接数据库");
				}else {
					if(input.length()==0) {
						JOptionPane.showMessageDialog(null, "请先输入金额");
					}else {
						int num=Integer.valueOf(input);
						if(num%100!=0) {
							JOptionPane.showMessageDialog(null, "存钱只能是100的整数倍");
						}else {
							
//							if(dt.isAlive()) {
//								JOptionPane.showMessageDialog(null, "账户正在执行操作请稍后");
//							}
							JOptionPane.showMessageDialog(null, "正在进行存款，请稍后");
							arlistactable=dbu.queryById();
							
							Actable a=arlistactable.get(0);
							st=new SaveThread(a, num);
							
							st.start();
							//使数据更新在操作完后进行
							//保证在线程结束后再进行读取数据
							try {
								st.sleep(4100);
							} catch (InterruptedException e1) {
								// TODO Aut-generated catch block
//								e1.printStackTrace();
								JOptionPane.showMessageDialog(null, "线程错误");
								
							}
							
							arlistinfotable.clear();
							arlistinfotable=dbu.query();
							Vector<String> v=new Vector<String>();
							v.add("Timest");
							v.add("Opertype");
							v.add("Money");
							DefaultTableModel df=new DefaultTableModel(v,0);
							for(Infotable in:arlistinfotable) {
								String[] line= {in.getTimest(),in.getOpertype(),String.valueOf(in.getMoney())};
								df.addRow(line);
							}
							table.setModel(df);
						}
						
							
						
					}
				}
			}
		});	
		
		//查询
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(con==null) {
					JOptionPane.showMessageDialog(null, "未连接数据库");
					
				}else {
					switch((EnTypeSearch)comboBox.getSelectedItem()) {
					case 操作时间:
						arlistinfotable.clear();
						arlistinfotable=dbu.queryByTime();

						break;
					case 交易类型:
						arlistinfotable.clear();
						arlistinfotable=dbu.queryByOperatype();
						break;
					}
					
					if(arlistinfotable.size()==0) {
						JOptionPane.showMessageDialog(null,"记录未空");
						
					}else {
						Vector<String> v=new Vector<String>();
						v.add("Timest");
						v.add("Opertype");
						v.add("Money");
						
						DefaultTableModel df=new DefaultTableModel(v,0);
						for(Infotable in:arlistinfotable) {
							String[] line= {in.getTimest(),in.getOpertype(),String.valueOf(in.getMoney())};
							df.addRow(line);
						}
						table.setModel(df);
					}
				}
			}
		});
		
		//序列化
		mntmSave.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(con==null) {
					JOptionPane.showMessageDialog(null, "未连接数据库");
					
				}else {
					JFileChooser jf=new JFileChooser();
					if(jf.showSaveDialog(MyExamGUI.this)==JFileChooser.APPROVE_OPTION) {
						String filename=jf.getSelectedFile().getAbsolutePath();
						if(arlistinfotable.size()==0) {
							JOptionPane.showMessageDialog(null, "无可存储数据");
							
						}else {
							myi.serSave(filename, arlistinfotable);
						}
					}
				}
			}
		});
		
		
	}
}
